package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.OrdenCompra;

public class OrdenCompraDAO {
	
	private static OrdenCompraDAO instancia = null;
	private static SessionFactory sf = null;

	public static OrdenCompraDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new OrdenCompraDAO();
		} 
		return instancia;
	}
	
	public void guardarOrdenCompra(OrdenCompra o){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(o);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarOrdenCompra(OrdenCompra o){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(o);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarOrdenCompra(OrdenCompra o){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(o);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<OrdenCompra> obtenerOrdenesCompras(){
		Session session = sf.openSession();
		Query q = session.createQuery("from OrdenesCompras");
		List<OrdenCompra> ordenesCompras = q.list();
		session.close();
		return ordenesCompras;
	}
}



